<%-- 
    Document   : index
    Created on : 27-mar-2013, 10.06.16
    Author     : Giuseppe
--%>

<%@page import="java.util.ArrayList"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="ejb.Videogame"%>
<%@page import="ejb.ArticoloNuovo"%>
<%@page import="java.util.LinkedList"%>
<%@page import="ejb.ArticoloUsato"%>
<%@page import="ejb.Piattaforma"%>
<%@page import="java.util.List"%>
<%@page import="ejb.Notizia"%>
<%@page import="ejb.RecensioneInterna"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@include file="wrapper.jsp" %>
<%
    String tipo = "uv";
    if (session.getAttribute("utente") != null) {
        Utente utSession = (Utente) session.getAttribute("utente");
        tipo = utSession.getTipo();
    }
%>

<div class="container">

    <div class="row-fluid">

        <div class="span3 bs-docs-sidebar">
            <ul class="nav nav-list bs-docs-sidenav">
                <% if (request.getParameter("platform") != null) {%>
                <li><a href="Controller?button=visualizzaRecNot&platform=<%=request.getParameter("platform")%>&type=news"> <button class="btn btn-large btn-inverse btn-block" type="button"><i class="icon-white icon-th-list"></i> Notizie</button> </a></li>
                <li><a href="Controller?button=visualizzaRecNot&platform=<%=request.getParameter("platform")%>&type=reviews"><button class="btn btn-large btn-inverse btn-block" type="button"><i class="icon-white icon-th-list"></i> Recensioni</button> </a></li>
                <li><a href="Controller?button=visualizzaRecNot&platform=<%=request.getParameter("platform")%>&type=both"><button class="btn btn-large btn-inverse btn-block" type="button"><i class="icon-white icon-th-list"></i> Entrambe</button> </a></li>
                <%} else {%>
                <li><a href="Controller?button=visualizzaRecNot&platform=all&type=news"> <button class="btn btn-large btn-inverse btn-block" type="button"><i class="icon-white icon-th-list"></i> Notizie</button> </a></li>
                <li><a href="Controller?button=visualizzaRecNot&platform=all&type=reviews"><button class="btn btn-large btn-inverse btn-block" type="button"><i class="icon-white icon-th-list"></i> Recensioni</button> </a></li>
                <li><a href="Controller?button=visualizzaRecNot&platform=all&type=both"><button class="btn btn-large btn-inverse btn-block" type="button"><i class="icon-white icon-th-list"></i> Entrambe</button> </a></li>
                <%}%>
            </ul>
        </div>

        <div class="span6">
            <ul style="margin: 0;padding: 0;list-style-type:none">
            <%
                Integer totCarrello = 0;
                SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
                LinkedList<ArticoloNuovo> carrello = (LinkedList<ArticoloNuovo>) session.getAttribute("carrello");
                List<ArticoloUsato> usatiDaConfermare = (List<ArticoloUsato>) session.getAttribute("usatiDaConfermare");

                List<Notizia> l = (List<Notizia>) request.getAttribute("listNotizie");
                List<String> testoNotizie = (List<String>) request.getAttribute("testoNotizie");
                List<Piattaforma> pl = null;
                List<RecensioneInterna> r = (List<RecensioneInterna>) request.getAttribute("listRecensioni");
                List<String> testoRec = (List<String>) request.getAttribute("testoRecensioni");
                List<Videogame> vg = null;

                int i = 0, j = 0;
                int index;
                Notizia not = null;
                RecensioneInterna ri = null;
                if (l == null) {
                    l = new ArrayList<Notizia>();
                }
                if (r == null) {
                    r = new ArrayList<RecensioneInterna>();
                }
                if (!l.isEmpty()) {
                    not = l.get(i);
                }
                if (!r.isEmpty()) {
                    ri = r.get(j);
                }
                while (i < l.size() || j < r.size()) {

                    if (j == r.size() || (i < l.size() && not.getData().before(ri.getData()))) {
            %>
            <li class="thumbnail">
                
                <a href="Controller?button=visualizzaNotizia&platform=<%=request.getParameter("platform")%>&selezionato=<%=not.getId()%>" >
                    <h2  style="padding-left: 10px;color: #1F1F1F"><%=not.getTitolo()%> <b style="padding-top: 10px; padding-right: 20px; font-family: italic;font-size: 15px;float:right" > Notizia </b></h2> </a>  
                    
                <img  src="<%=not.getImmaginePath()%>" align="left" alt="" style="padding-left:10px;padding-top:5px;height:200px;width: 430px">

                <div class="caption">
                    
                    <h5>   <%
                        pl = not.getListPiattaforme();
                        for (Piattaforma p : pl) {
                            out.print(p.getNome() + " ");
                        }
                        index = (testoNotizie.get(i).length() < 100) ? testoNotizie.get(i).length() : 100;
                        %> <br> 
                        Autore: <%=not.getAutore().getNome()%> <%=not.getAutore().getCognome()%>   <br>  Data: <%=dateFormat.format(not.getData())%> </h5>
                        
                    <p><%=testoNotizie.get(i).substring(0, index) + "..."%></p>
                </div>
            </li>
            <% if (i < l.size() - 1) {
                    not = l.get(++i);
                } else {
                    i++;
                }
            } else {

            %>                

            <li class="thumbnail">
                <a href="Controller?button=visualizzaRecensione&platform=<%=request.getParameter("platform")%>&selezionato=<%=ri.getId()%>"> 
                    <h2  style="padding-left: 10px;color: #1F1F1F"><%=ri.getTitolo()%> <b style="padding-top: 10px; padding-right: 20px; font-family: italic;font-size: 15px;float:right" > Recensione </b></h2></a>

                <img src="<%=ri.getImmagini().get(0)%>" align="left" alt="" style="padding-left:10px;padding-top:5px;height:200px;width: 430px">
                <div class="caption">
                    <h5 >   <%
                        vg = ri.getVideogiochi();
                        for (Videogame v : vg) {
                            out.print(v.getNomeEPiattaforma() + " ");
                        }
                        index = (testoRec.get(j).length() < 100) ? testoRec.get(j).length() : 100;
                        %> 
                        <br> 
                        Autore: <%=ri.getAutore().getNome()%> <%=ri.getAutore().getCognome()%>   <br>  Data: <%=dateFormat.format(ri.getData())%></h5>

                    <p><%=testoRec.get(j).substring(0, index) + "..."%></p>
                </div>

            </li>
            <% if (j < r.size() - 1) {
                            ri = r.get(++j);
                        } else {
                            j++;
                        }
                    }
                }
            %>

            </ul>
        </div> 

        <div class="span3">
            <%
                if (tipo.equals("u")) {
            %>
            <div class="thumbnail">
                <p><b>Carrello</b></p>
                <table>
                    <tr><td></td><td style="padding-left: 5px"><b>Articolo</b></td><td style="padding-left: 5px"><b>Costo</b></td></tr>
                    <tr><td><br></td><td><br></td><td><br></td></tr>
                            <%
                                for (ArticoloNuovo a : carrello) {
                                    totCarrello = totCarrello + a.getPrezzo();
                            %>
                    <tr>
                    <p>
                    <td>
                        <a href="Controller?button=removeCarrello&amp;idArticolo=<%=a.getId()%>">
                            <button class="btn btn-inverse btn-mini"><i class="icon-white icon-trash"></i></button>
                        </a>
                    </td>
                    <td style="padding-left: 5px;height: 20px;width:100%;overflow:hidden;float:left"><%=a.getNome()%></td>
                    <td style="padding-left: 5px"><%=a.getPrezzo().toString()%> €</td>
                    </p>
                    </tr>
                    <tr><td><br></td><td><br></td><td><br></td></tr>
                            <%}%>
                    <tr><td></td><td><b>Totale</b></td><td style="padding-left: 5px"><%=totCarrello.toString()%> €</td></tr>
                </table>
            </div>
            <br>
            <br>
            <%} else if (tipo.equals("a")) {%>
            <div class="thumbnail">
                <p><b>Notes</b></p>
                <br>
                <% if (!usatiDaConfermare.isEmpty()) {%>
                <p> Ancora <%=usatiDaConfermare.size()%> articoli da confermare </p>
                <br>
                <p align="center">
                    <a href="Controller?button=modificaCancellaArtUsati&amp;confirm=s">
                        <button class="btn btn-inverse btn-small"><i class="icon-white icon-th-list"></i> Vedi Articoli da confermare</button>
                    </a>
                </p>
                <%} else {%>
                <p> Nessun articolo da confermare </p>
                <%}%>      
            </div>
            <br>
            <br>
            <%}%>

            <form  class="navbar-search" action="Controller"> <input type="text" name="cerca" class="search-query span3" style="width:220px;" placeholder="Cerca"><div class="icon-search" ></div></form>
        </div>          

        <!--
        <iframe src="https://www.google.com/calendar/embed?src=f7ni105q3cfp27s1t374amoqug%40group.calendar.google.com&ctz=Europe/Rome" style="border: 0" width="240" height="180" frameborder="0" scrolling="no" align="right">></iframe>-->


    </div>
</div>
</body>
</html>
